package org.czkt.controller;


import org.czkt.pojo.AppCategory;
import org.czkt.pojo.AppVersion;
import org.czkt.pojo.DataDictionary;
import org.czkt.pojo.vo.AppInfoVo;
import org.czkt.pojo.vo.AppQuery;
import org.czkt.pojo.vo.BackendUserVo;
import org.czkt.service.*;
import org.czkt.util.AppFinalClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager")
public class BackendController {
    @Autowired
    BackendUserService backendUserService;

    @Autowired
    DataDictionaryService dataDictionaryService;

    @Autowired
    AppCategoryService appCategoryService;
    @Autowired
    AppInfoService appInfoService;

    @Autowired
    AppVersionService appVersionService;

    @RequestMapping("/login")
    public Object login() {
        return "backendlogin";
    }

    @RequestMapping("/dologin")
    public Object doLogin(String userCode, String userPassword, Model model, HttpSession session) {
        //1、获取用户名和密码(springMVC绑定到了形参上)


        //2、调用service方法根据用户名和密码查询用户
        BackendUserVo backendUser = backendUserService.getBackendUserByCodeAndPwd(userCode, userPassword);

        //3、如果查询到则将当前用户保存到sesession 并跳转主页   当前不存返回错误信息至登录页

        if (backendUser == null) {
            model.addAttribute("error", "账号或密码错误！");
            return "backendlogin";
        } else {
            session.setAttribute("userSession", backendUser);
            return "backend/main";
        }


    }


    @RequestMapping("/logout")
    public Object logout(HttpSession session) {
        session.removeAttribute("userSession");
        return "backendlogin";
    }


    @RequestMapping("/backend/app/list")
    public Object getAppList(
            AppQuery appQuery,
            @RequestParam(defaultValue = "1") int pageIndex,
            Model model) {
        //查询所属平台数据
        List<DataDictionary> dataDictionaries = dataDictionaryService.getListByCode(AppFinalClass.APP_FLATFORM);

        //Stream
        //查询一级分类
        List<AppCategory> categoryList1 = appCategoryService.getListByParentId(null);
        List<Long> parentIds = new ArrayList<>();
        for (int i = 0; i < categoryList1.size(); i++) {
            parentIds.add(categoryList1.get(i).getId());
        }


        //查询二级分类
        List<AppCategory> categoryList2 = appCategoryService.getListByParentIds(parentIds);

        parentIds = new ArrayList<>();
        for (int i = 0; i < categoryList2.size(); i++) {
            parentIds.add(categoryList2.get(i).getId());
        }
        //查询三级分类
        List<AppCategory> categoryList3 = appCategoryService.getListByParentIds(parentIds);

        int pageSize = 5;

        List<AppInfoVo> appInfoList = appInfoService.getList(appQuery, pageIndex, pageSize);


        int rowCount = appInfoService.getCount(appQuery);


        model.addAttribute("flatFormList", dataDictionaries);
        model.addAttribute("categoryLevel1List", categoryList1);
        model.addAttribute("categoryLevel2List", categoryList2);
        model.addAttribute("categoryLevel3List", categoryList3);
        model.addAttribute("appInfoList", appInfoList);


        Map<Object, Object> pages = new HashMap<>();

        //总页数
        pages.put("totalPageCount", rowCount % pageSize == 0 ? rowCount / pageSize : (rowCount / pageSize) + 1);
        //总记录数
        pages.put("totalCount", rowCount);
        //当前页面
        pages.put("currentPageNo", pageIndex);
        //每页显示的记录数
//        pages.put("",);
        model.addAttribute("pages", pages);
        model.addAttribute("appQuery", appQuery);

        return "backend/applist";

    }


    @GetMapping("/backend/app/categorylevellist.json")
    @ResponseBody
    public Object getAppList(Integer pid) {
        //查询下级分类
        List<AppCategory> categoryList1 = appCategoryService.getListByParentId(pid);
        return categoryList1;

    }

    /**
     * 查询信息并跳转到审核页
     *
     * @param aid
     * @param vid
     * @return
     */
    @GetMapping("/backend/app/check")
    public Object getAppList(Long aid, Long vid, Model model) {

        AppInfoVo appInfo = appInfoService.getAppInfoById(aid);
//        if(vid!=0){
        AppVersion appVersion = appVersionService.getAppVersionById(vid);
        model.addAttribute("appVersion", appVersion);
//        }

        model.addAttribute("appInfo", appInfo);

        return "backend/appcheck";

    }

    /**
     * 查询信息并跳转到审核页
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/backend/app/checksave")
    public Object checksave(Long status, Long id, Model model) {
        appInfoService.updateAppInfo(id, status);
        return "redirect:/manager/backend/app/list";

    }
}
